import 'dart:async';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../util/print_util.dart';

class MyAnimationPage extends StatefulWidget {
  const MyAnimationPage({super.key});

  @override
  State<MyAnimationPage> createState() => _MyAnimationPageState();
}

class _MyAnimationPageState extends State<MyAnimationPage>
    with SingleTickerProviderStateMixin {
  static const mTAG = "_MyAnimationPageState";
  late AnimationController _controller;
  late Animation<double> animation;
  late ColorTween colorAnimation;
  late CurvedAnimation mCurves;

  @override
  void initState() {
    _controller = AnimationController(
        value: 150,
        vsync: this,
        duration: const Duration(seconds: 5),
        reverseDuration: const Duration(seconds: 10))
      ..repeat(reverse: true);
    mCurves = CurvedAnimation(parent: _controller, curve: Curves.bounceInOut);
    animation = Tween(begin: 100.0, end: 300.0).animate(mCurves);
    animation.addListener(() {
      setState(() {});
    });

    // colorAnimation = ColorTween(begin: Colors.amber, end: Colors.red);
    // colorAnimation.animate(mCurves);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        FilledButton(
            onPressed: () {
              LogUtils.d("onPressed", mTAG);
              _controller.forward();
              if (_controller.status == AnimationStatus.forward) {
                _controller.forward();
              } else {
                _controller.reverse();
              }
            },
            child: Text("开启动效")),
        MyAnimationWidget(animation)
      ],
    );
  }
}

class MyAnimationWidget extends AnimatedWidget {
  final Animation mAnimation;

  const MyAnimationWidget(this.mAnimation, {super.key})
      : super(listenable: mAnimation);

  @override
  Widget build(BuildContext context) {
    Animation animation = listenable as Animation;
    return Container(
      width: animation.value,
      height: animation.value,
      color: Colors.red,
      child: const Text("animation"),
    );
  }
}

///-----------------------------AnimatedContainer---------------------------------------

class AnimatedContainerPage extends StatefulWidget {
  const AnimatedContainerPage({super.key});

  @override
  State<AnimatedContainerPage> createState() => _AnimatedContainerPageState();
}

class _AnimatedContainerPageState extends State<AnimatedContainerPage> {
  late double height;
  late double width;
  Color color1 = Colors.red.shade50;
  double radius = 10;

  @override
  void initState() {
    height = 100;
    width = 100;
    // Timer(const Duration(milliseconds: 100), () {
    //   height = 1000;
    //   width = 1000;
    //   color1 = Colors.red;
    //   radius = 0;
    // });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Timer(const Duration(milliseconds: 100), () {
      height = 1000;
      width = 1000;
      color1 = Colors.red;
      radius = 0;
    });
    return Scaffold(
        appBar: AppBar(
          title: const Text("AnimatedContainerPage"),
        ),
        body: AnimatedContainer(
          duration: const Duration(milliseconds: 10000),
          width: width,
          height: height,
          decoration: BoxDecoration(
              color: color1,
              borderRadius: BorderRadius.all(Radius.circular(radius))),
        ));
  }
}

///-----------------------------MultiAnimationPage 交织动画---------------------------------------

class MultiAnimationPage extends StatefulWidget {
  const MultiAnimationPage({super.key});

  @override
  State<MultiAnimationPage> createState() => _MultiAnimationPageState();
}

class _MultiAnimationPageState extends State<MultiAnimationPage>
    with SingleTickerProviderStateMixin {
  static const mTAG = "_MyAnimationPageState";
  late AnimationController _controller;
  late Animation<double> animation;
  late Animation<double> rotationAnimation;
  late Animation<Color?> colorAnimation;
  late CurvedAnimation mCurves;

  @override
  void initState() {
    _controller = AnimationController(
        value: 150,
        vsync: this,
        duration: const Duration(seconds: 5),
        reverseDuration: const Duration(seconds: 10))
      ..repeat(reverse: true);
    mCurves = CurvedAnimation(parent: _controller, curve: Curves.linear);
    animation = Tween(begin: 100.0, end: 300.0).animate(mCurves);
    animation.addListener(() {
      setState(() {});
    });

    colorAnimation =
        ColorTween(begin: Colors.amber, end: Colors.red).animate(_controller);
    rotationAnimation =
        Tween<double>(begin: 0, end: 2 * pi).animate(_controller);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        FilledButton(
            onPressed: () {
              LogUtils.d("onPressed", mTAG);
              _controller.forward();
              if (_controller.status == AnimationStatus.forward) {
                _controller.forward();
              } else {
                _controller.reverse();
              }
            },
            child: Text("开启动效")),
        AnimatedBuilder(
            animation: _controller,
            builder: (context, child) {
              return Opacity(
                  opacity: mCurves.value,
                  child: Transform(
                    alignment: Alignment.center,
                    transform: Matrix4.rotationZ(rotationAnimation.value),
                    child: Container(
                      height: animation.value,
                      width: animation.value,
                      color: colorAnimation.value,
                      child: const Center(
                        child: Text(
                          "福",
                          style: TextStyle(fontSize: 50),
                        ),
                      ),
                    ),
                  ));
            }),
      ],
    );
  }
}

///-----------------------------AnimatedSwitcher--------------------------------------
class AnimatedSwitcherPage extends StatefulWidget {
  const AnimatedSwitcherPage({super.key});

  @override
  State<AnimatedSwitcherPage> createState() => _AnimatedSwitcherPageState();
}

class _AnimatedSwitcherPageState extends State<AnimatedSwitcherPage> {
  Color color = Colors.red;

  @override
  void initState() {
    Future.delayed(const Duration(milliseconds: 10), () {
      color = Colors.orange;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("AnimatedSwitcherPage"),
        ),
        body: AnimatedContainer(
          width: 400,
          height: 400,
          duration: const Duration(milliseconds: 500),
          curve: Curves.bounceInOut,
          color: color,
          child: Center(
            child: AnimatedSwitcher(
                reverseDuration: const Duration(milliseconds: 2000),
                duration: const Duration(milliseconds: 4000),
                child: Text(
                  "WWIIISSSSSS",
                  // "99999999999",
                  style: const TextStyle(fontSize: 30, color: Colors.white),
                  key: UniqueKey(),
                ),
                transitionBuilder: (child, animation) {
                  return FadeTransition(
                      opacity: animation,
                      child: ScaleTransition(
                        scale: animation,
                        child: RotationTransition(
                            turns: animation,
                            child: SizeTransition(
                              sizeFactor: animation,
                              child: child,
                            )),
                      ));
                }),
          ),
        ));
  }
}

///-----------------------------TweenAnimationBuilderPage--------------------------------------
class TweenAnimationBuilderPage extends StatefulWidget {
  const TweenAnimationBuilderPage({super.key});

  @override
  State<TweenAnimationBuilderPage> createState() =>
      _TweenAnimationBuilderPageState();
}

class _TweenAnimationBuilderPageState extends State<TweenAnimationBuilderPage> {
  static const _sTag = "TweenAnimationBuilderPage";
  Color color = Colors.red;

  double endValue = 100;

  late Tween<double> tween;

  @override
  void initState() {
    Future.delayed(const Duration(milliseconds: 10), () {
      color = Colors.orange;
    });
    tween = Tween(begin: 0, end: endValue);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("AnimatedSwitcherPage"),
      ),
      body: TweenAnimationBuilder(
        tween: Tween<double>(begin: 0, end: endValue),
        duration: const Duration(milliseconds: 5000),
        curve: Curves.ease,
        builder: (BuildContext context, value, Widget? child) {
          return Container(
            width: value,
            height: value,
            color: Colors.orange,
            child: Center(
                child: Transform.scale(
              scale: value / 100,
              child: const Text("Tween"),
            )

                //   child: Transform.rotate(
                // angle: value,
                // child: const Text(
                //   "Tween",
                //   style: TextStyle(fontSize: 30),
                // ),

                //     child: Transform.translate(
                //   offset: Offset(value, value),
                //   child: const Text(
                //     "Tween",
                //     style: TextStyle(fontSize: 30),
                //   ),
                // )

                //     child: Transform.flip(
                //   // flipX: true,
                //   flipY: true,
                //   child: const Text(
                //     "Tween",
                //     style: TextStyle(fontSize: 30),
                //   ),
                // )

                ),
          );
        },
      ),
      bottomSheet: Row(
        children: [
          FilledButton(
            onPressed: () {
              endValue += 100;
              setState(() {
                // tween = Tween(begin: 0, end: endValue);
              });
              LogUtils.d(_sTag, "build end:$endValue");
            },
            child: const Text("+"),
          ),
          FilledButton(
            onPressed: () {
              setState(() {
                if (endValue > 200) {
                  endValue -= 100;
                }
                // tween = Tween(begin: 0, end: endValue);
              });
              LogUtils.d(_sTag, "build end:$endValue");
            },
            child: const Text("-"),
          )
        ],
      ),
    );
  }
}
